package com.zjmzxfzhl.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.zjmzxfzhl.common.core.exception.SysException;
import com.zjmzxfzhl.modules.app.entity.*;
import com.zjmzxfzhl.modules.app.form.AppTaskUserVo;
import com.zjmzxfzhl.modules.app.service.AppProjUserService;
import com.zjmzxfzhl.modules.app.service.AppTaskService;
import com.zjmzxfzhl.modules.sys.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zjmzxfzhl.common.core.base.BaseServiceImpl;
import com.zjmzxfzhl.modules.app.mapper.AppTaskUserMapper;
import com.zjmzxfzhl.modules.app.service.AppTaskUserService;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Service
 * 
 * @author 张森荣
 */
@Service
public class AppTaskUserServiceImpl extends BaseServiceImpl<AppTaskUserMapper, AppTaskUser>
        implements AppTaskUserService {
    @Autowired
    private AppProjUserService appProjUserService;
    @Autowired
    private AppTaskService appTaskService;

    @Override
    public IPage<AppTaskUserVo> list(IPage<AppTaskUserVo> page, AppTaskUserVo appTaskUserVo) {
        return page.setRecords(baseMapper.list(page, appTaskUserVo));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveAppTaskUser(AppTaskUser appTaskUser) {
        Map<SFunction<AppTaskUser,?>,Object> params=new HashMap<>();
        if(appTaskUser.getTaskId()!=null&&!"".equals(appTaskUser.getTaskId())){
            params.put(AppTaskUser::getTaskId,appTaskUser.getTaskId());
        }
        if(appTaskUser.getUserId()!=null&&!"".equals(appTaskUser.getUserId())){
            params.put(AppTaskUser::getUserId,appTaskUser.getUserId());
        }
        Integer total=this.count(new LambdaQueryWrapper<AppTaskUser>().allEq(params));
        if(total>0){
            return false;
        }

        //判断是否存在非父项目成员
        AppTask appTask=appTaskService.getById(appTaskUser.getTaskId());
        if (appTask.getProjId() != null && !"".equals(appTask.getProjId())) {
            List<SysUser> userList=new ArrayList<>();
            SysUser sysUser=new SysUser();
            sysUser.setUserId(appTaskUser.getUserId());
            userList.add(sysUser);
            if (!appProjUserService.checkMembers(userList, appTask.getProjId())) {
                throw new SysException("存在不属于父项目的参与成员！");
            }
        }

        appTaskUser.setId(UUID.randomUUID().toString().replace("-", "").toLowerCase());
        Boolean result=this.save(appTaskUser);
        return result;
    }

    /**
     * 新增项目，自动计算机构级别、机构级次码、是否叶子
     *
     * @param appTaskUser
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean relationDel(AppTaskUser appTaskUser) {
        Map<String, Object> columnMap=new HashMap<>();
        if (appTaskUser.getTaskId() == null && appTaskUser.getUserId() == null) {
            return false;
        }
        if(appTaskUser.getTaskId()!=null){
            columnMap.put("TASK_ID",appTaskUser.getTaskId());
        }
        if(appTaskUser.getUserId()!=null){
            columnMap.put("USER_ID",appTaskUser.getUserId());
        }
        Boolean result=this.removeByMap(columnMap);
        return result;
    }
}
